#!/usr/bin/env python
#coding:utf-8
"""
  Author:  xulei --<>
  Purpose: 加密模块
  Created: 09/03/18
"""

import sys
try:
    if sys.version_info.major<3:
        from urllib import quote, unquote
    else:
        from urllib.parse import quote, unquote
except:
    pass
import base64
import bcrypt
# 用于auth加密与解密
# 密钥
authkey = "ulei"
# 异或算法
xorStr = lambda ss,cc: ''.join(chr(ord(s)^ord(cc)) for s in ss)
# 方法
def encrypt_auth(data,key=authkey):
    """数据加密"""
    enStr = data   
    for cc in key:
        enStr = xorStr(enStr,cc)
    if isinstance(enStr, str):
        enStr = enStr.encode("utf-8")     
    return quote(base64.b64encode(enStr))

def decrypt_auth(data,key=authkey):
    """数据解密"""
    data = unquote(unquote(data))
    deStr = base64.b64decode(unquote(data))
    deKey = key[::-1]
    if isinstance(deStr, bytes):
        deStr = deStr.decode("utf-8")    
    for cc in deKey:
        deStr = xorStr(deStr,cc)
    return deStr

# import json
#fCVkbmMlPSclPzIwJSsnJWV1ZmljRGhjYiU9JyVxcGZgJXo%3D
# content= decrypt_auth("DVQVHxJUTFZUVAs%3D",'buick')
# print(content)00935027106
# content= decrypt_auth("Yjt6dn18OyM5OyotKDtk",'audi')
# print(content)

# content.update({'brandCode':brandCode})
# print(type(content),content)
# print(json.loads(content))
#print(encrypt_auth(json.dumps({"cid": "745"}),'audi'))
# print(encrypt_auth('{"cid": "GEN080TA00"}','hyundai'))


#print(epc_user_pwd('28351770'.encode()))